﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Domain;
using Customer.Infrstructre.FinanceManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 修改财务类型中介者
    /// </summary>
    public class UpdFinanceCommandHandler:IRequestHandler<UpdFinanceCommand,int>
    {
        private readonly IFinanceTypeRepository _financeTypeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="financeTypeRepository"></param>
        public UpdFinanceCommandHandler(IFinanceTypeRepository financeTypeRepository)
        {
            _financeTypeRepository = financeTypeRepository;
        }

        public async Task<int> Handle(UpdFinanceCommand request, CancellationToken cancellationToken)
        {
            var res = await _financeTypeRepository.GetAsync(x => x.Office_Id == request.Office_Id && x.User_Id == request.User_Id && x.FinanceTypeName == request.FinanceTypeName);
            if (res.Count() > 0)
            {
                return -1;//财务类别名称已存在
            }
            var financeModel = new FinanceType() { FinanceTypeId = request.FinanceTypeId, FinanceTypeCode = request.FinanceTypeCode, FinanceTypeName = request.FinanceTypeName, FRremark = request.FRremark, Office_Id = request.Office_Id, User_Id = request.User_Id, AddName = request.AddName, AddTime = request.AddTime, UpdName = request.UpdName , UpdTime =request.UpdTime };
            return await _financeTypeRepository.Update(financeModel);
        }
    }
}
